<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true">
      <el-form-item prop="search">
        <el-input
            v-model="queryParams.search"
            placeholder="请输入IP或端口或telnetIp"
            clearable
            style="width: 240px"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="端口状态" prop="status">
        <el-select
            v-model="queryParams.status"
            placeholder="端口状态"
            clearable
        >
          <el-option
              v-for="dict in status_list"
              :key="dict.value"
              :label="dict.lable"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
      <el-form-item style="float: right;">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['system:port:add']"
        >新增</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格数据 -->
    <el-table v-loading="loading" :data="portList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" width="55">
        <template #default="scope">{{scope.$index+1}}</template>
      </el-table-column>
      <el-table-column label="IP" prop="ip" />
      <el-table-column label="Telnet IP" prop="telnetIp" />
      <el-table-column label="Telnet端口">
        <template #default="scope">
          <span v-if="scope.row.portState!=2">{{scope.row.telnetPort}}</span>
          <el-tooltip placement="top" content="可能已下线" v-if="scope.row.portState==2">
            <span class="scope_proc_red">{{scope.row.telnetPort}}</span>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="名称" prop="name" />
      <el-table-column label="端口状态">
        <template #default="scope">
          <span v-if="scope.row.portState==1" class="scope_percent">正常</span>
          <span v-if="scope.row.portState==2" class="scope_proc_red">失败</span>
        </template>
      </el-table-column>
      <el-table-column label="监控状态">
        <template #default="scope"><span class="scope_percent">监控中</span></template>
      </el-table-column>
      <el-table-column label="更新时间" prop="updateTime" width="160" />
      <el-table-column label="操作" align="center" width="170" >
        <template #default="scope">
          <el-button
              @click="tableEdit(scope.row)"
              class="edit_btn"
              v-hasPermi="['system:port:edit']">编辑</el-button>
          <el-button
              @click="tableDelete(scope.row)"
              class="delete_btn"
              v-hasPermi="['system:port:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getData"
    />

    <!-- 添加或修改 -->
    <el-dialog :title="title" v-model="openStatus" width="500px" append-to-body>
      <el-form ref="roleRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="IP" prop="ip">
          <el-input v-model="form.ip" placeholder="请输入IP" auto-complete="off" @input="inputVal" />
        </el-form-item>
        <el-form-item label="Telnet IP" prop="telnetIp">
          <el-input v-model="form.telnetIp" placeholder="请输入Telnet IP" auto-complete="off" @input="inputVal" />
        </el-form-item>
        <el-form-item label="Telnet端口" prop="telnetPort">
          <el-input v-model="form.telnetPort" placeholder="请输入Telnet端口" auto-complete="off" @input="inputVal" />
        </el-form-item>
        <el-form-item label="端口名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入端口名称" auto-complete="off" @input="inputVal" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <p class="lianjie_btn" @click="validateRow">端口测试</p>
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancelForm">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { getList, updateData, deleteRow, validate } from "@/api/system/port";

const router = useRouter();
const { proxy } = getCurrentInstance();
const loading = ref(true);
const portList = ref([]);
const ids = ref([]);
const total = ref(0);
const title = ref("");
const openStatus = ref(false);
const connectionHand = ref(false);
const connectionStatus = ref(false);
const status_list = ref([{lable:'全部',value:0},{lable:'在线',value:1},{lable:'已下线',value:2}]);
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    status: 0,
    search: ''
  },
  rules: {
    ip: [{ required: true, message: "端口IP不能为空", trigger: "blur" }],
    telnetIp: [{ required: true, message: "Telnet IP不能为空", trigger: "blur" }],
    telnetPort: [{ required: true, message: "Telnet端口不能为空", trigger: "blur" }],
    name: [{ required: true, message: "端口名称不能为空", trigger: "blur" }]
  }
});
const { queryParams, form, rules } = toRefs(data);

/** 查询列表数据 */
function getData() {
  loading.value = true;
  getList(queryParams.value).then(response => {
    portList.value = response.data.records;
    total.value = response.data.total;
    loading.value = false;
  });
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getData();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

//操作-编辑
function tableEdit(data){
  reset();
  openStatus.value = true;
  title.value = "编辑端口信息";
  form.value = data;
}

//操作-删除
function tableDelete(data){
  proxy.$modal.confirm('是否确认删除此数据').then(function () {
    return deleteRow(data.id);
  }).then(() => {
    getData();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

//新增
function handleAdd(){
  reset();
  openStatus.value = true;
  title.value = "新增端口信息";
}

//新增-确定
function submitForm(){
  proxy.$refs["roleRef"].validate(valid => {
    if(valid){
      console.log(connectionHand.value)
      console.log(connectionStatus.value)
      if(!connectionHand.value){
        proxy.$modal.msgError("请先操作端口测试");
        return
      }
      if(!connectionStatus.value){
        proxy.$modal.msgError("端口测试失败，不能提交");
        return
      }

      updateData(form.value).then(response => {
        if(form.value.id){
          proxy.$modal.msgSuccess("修改成功");
        }else{
          proxy.$modal.msgSuccess("保存成功");
        }
        openStatus.value = false;
        getData();
      });
    }
  });
}

//新增-取消
function cancelForm(){
  openStatus.value = false;
  reset();
}

//连接测试
function validateRow(){
  proxy.$refs["roleRef"].validate(valid => {
    if(valid){
      validate(form.value).then(response => {
        connectionHand.value = true;
        if(response.data==false){
          connectionStatus.value = false;
          proxy.$modal.msgError("测试失败");
          return
        }
        connectionStatus.value = true;
        proxy.$modal.msgSuccess("测试成功");
      }).catch(err => {
        connectionStatus.value = false;
        proxy.$modal.msgError("测试失败");
        connectionHand.value = true;
      })
    }
  });
}

//监听数据变化
function inputVal(){
  connectionStatus.value = false;
  connectionHand.value = false;
}

/** 重置新增的表单以及其他数据  */
function reset() {
  connectionHand.value = false;
  connectionStatus.value = false;
  form.value = {
    ip: '',
    telnetIp: '',
    telnetPort: '',
    name: ''
  };
  proxy.resetForm("roleRef");
  getData();
}
getData();
</script>

<style>
.scope_percent{
  background: #409eff;
  padding: 3px 6px;
  color: #fff;
  font-size: 12px;
  border-radius: 3px;
}
.scope_proc_red{
  font-weight: bold;
  background: #e15361;
  padding: 3px 6px;
  color: #fff;
  font-size: 12px;
  border-radius: 3px;
}
.edit_btn,.delete_btn{
  width: 60px;
  height: 32px;
  text-align: center;
  line-height: 32px;
  color: #fff;
  font-size: 14px;
  border-radius: 4px;
  border: 0;
}
.edit_btn{
  background: #ec8a18;
}
.delete_btn{
  background: #e15361;
}
.delete_btn:hover{
  background: rgba(225, 83, 97, 0.8);
  color: #fff;
}
.edit_btn:hover{
  background: rgba(236, 138, 24, 0.8);
  color: #fff;
}
.lianjie_btn{
  float: left;
  font-size: 14px;
  line-height: 32px;
  padding-left: 20px;
  color: #409eff;
  font-weight: bold;
  text-decoration: underline;
  margin: 0;
  cursor: pointer;
}
</style>
